<?php

global $connect;

if (isset($_GET['id_mag']) AND $_GET['id_mag'] != NULL)
{
	$sql = "SELECT `NMAGASIN` AS 'id', `NOM` AS 'nom' FROM `magasins` WHERE `NMAGASIN` != '".$_GET['id_mag']."'";
	$result = mysql_query($sql) or die(mysql_error());
	echo "<form method='post' action=index.php?page=magasins&action=transfert&id_mag=".$_GET['id_mag']."><p>DE  <select name='m_out'>";
	while ($data = mysql_fetch_array($result))
	{
		if (isset($_POST['m_out']) AND $_POST['m_out'] == $data['id'])
			echo "<option value=".$data['id']." SELECTED>".$data['nom']."</option>";
		else
			echo "<option value=".$data['id'].">".$data['nom']."</option>";
	}
	$sql = "SELECT `NOM` AS 'nom' FROM `magasins` WHERE `NMAGASIN` = '".$_GET['id_mag']."'";
	$result = mysql_query($sql) or die(mysql_error());
	$data = mysql_fetch_array($result);
	echo "</select>  VERS  ".$data['nom']."   <input type='submit' value='Valider'></p></form>";
}

if (isset($_POST['m_out']))
{
	$mag_out = $_POST['m_out'];
	$mag_in = $_POST['m_in'];

	foreach($_POST AS $key=>$value)
	{
		if ($key != 'm_out' && $key != 'm_in')
		{	
			$sql = "INSERT INTO `approvisionnement` (`NMAGASIN_OUT`, `NMAGASIN_IN`, `NEMPLOYE`, `NPRODUIT`, `QUANTITE`) 
			VALUES ('".$mag_out."', '".$mag_in."', '1', '".$key."', '".$value."') ";
			echo "$sql<hr /><br />";
			$res = mysql_query($sql) or die(mysql_error());
			
			$sql = "SELECT `QUANTITE` FROM `emplacements` WHERE `NMAGASIN` = '".$mag_out."' AND `NPRODUIT` = '".$key."'";
			echo "$sql<hr /><br />";
			$res = mysql_query($sql) or die(mysql_error());
			$data = mysql_fetch_array($res);
			$tab['out'] = $data['QUANTITE'];
			
			$sql = "SELECT `QUANTITE` FROM `emplacements` WHERE `NMAGASIN` = '".$mag_in."' AND `NPRODUIT` = '".$key."'";
			echo "$sql<hr /><br />";
			$res = mysql_query($sql) or die(mysql_error());
			$data = mysql_fetch_array($res);
			$tab['in'] = $data['QUANTITE'];
			
			$sql = "UPDATE `emplacements` SET `QUANTITE` = '".($value - $tab['out'])."' WHERE `NPRODUIT` = '".$key."' AND `NMAGASIN` = '".$mag_out."'";
			echo "$sql<hr /><br />";
			mysql_query($sql) or die(mysql_error());
			
			$sql = "UPDATE `emplacements` SET `QUANTITE` = '".($value + $tab['in'])."' WHERE `NPRODUIT` = '".$key."' AND `NMAGASIN` = '".$mag_in."'";
			echo "$sql<br />";
			mysql_query($sql) or die(mysql_error());
		}	
	}
	echo "<div class='box_success'>Approvisionement Valid&eacute;</div>";
}

echo "<br /><br />";

if (isset($_POST['m_out']))
{
	$sql = "SELECT p.MARQUE AS 'nom', p.NPRODUIT AS 'id_p', e.CELLULE AS 'cellule', e.QUANTITE AS 'quantite' 
	FROM `emplacements` e LEFT JOIN `produits` p ON (p.NPRODUIT = e.NPRODUIT) 
	WHERE e.NMAGASIN = '".$_POST['m_out']."'";
	$result = mysql_query($sql) or die(mysql_error());
	echo "<center><form action='index.php?page=magasins&action=transfert&id_mag=".$_GET['id_mag']."' method='post'><table>";
	echo "<thead><tr><th>Produit</th><th>Cellule</th><th>Quantite</th><th>Prendre</th></tr></thead>";
	while ($data = mysql_fetch_array($result))
	{
		echo "<tr>";
		echo "<td>".$data['nom']."</td>";
		echo "<td>".$data['cellule']."</td>";
		echo "<td>".$data['quantite']."</td>";
		echo "<td><input type='text' name='".$data['id_p']."' value='0'><td>";
		echo "</tr>";
	}
	echo "<input type='hidden' value=".$_POST['m_out']." name='m_out' />";
	echo "<input type='hidden' value=".$_GET['id_mag']." name='m_in' />";
	echo "</table><input type='submit' value='Valider'></form></center>";
}

?>

<center><h1><a href="index.php?page=magasins&show_mag_id=<?php echo $_GET['id_mag'] ?>"><img width="30px" height="30px" src="./img/jamembo-back.png" />Retour</a></h1></center>
